<template>
  <div>
    <el-form ref="tableForm" :inline="true" :model="tableForm" class="demo-ruleForm">
      <el-form-item label="日志类型">
        <el-input v-model="tableForm.type"/>
      </el-form-item>
      <el-form-item label="日志级别">
        <el-input v-model="tableForm.level"/>
      </el-form-item>
      <el-form-item label="日志内容">
        <el-input v-model="tableForm.log"/>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="tableForm.createTimeRange" end-placeholder="结束日期" range-separator="至"
          start-placeholder="开始日期" type="datetimerange"
        />
      </el-form-item>
      <el-button type="primary" @click="page()"> 查询</el-button>
      <el-button @click="resetPageRequestBody()"> 重置</el-button>
    </el-form>

    <el-button type="primary" @click="deleteAll()">
      清空日志
    </el-button>

    <el-table :data="tableList" border>
      <el-table-column fixed label="ID" prop="id" min-width="50"/>
      <el-table-column label="类型" prop="type" min-width="80"/>
      <el-table-column label="级别" prop="level" min-width="80"/>
      <el-table-column label="日志" prop="log" min-width="500">
        <template slot-scope='scope'>
          <el-popover placement="top-start" width="400" trigger="hover">
            <span>{{scope.row.log}}</span>
            <span slot="reference">{{scope.row.log.substr(0,30) + '...'}}</span>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="createTime" min-width="160"/>
      <el-table-column label="更新时间" prop="updateTime" min-width="160"/>
      <el-table-column label="操作" fixed="right" width="100">
        <template v-slot="scope">
          <el-button type="text" size="small" @click="_delete(scope.row.id)"> 删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination :current-page="pageForm.current" :total="pageForm.total" :page-size="pageForm.size"
                   :page-sizes="pageForm.sizes"
                   :style="pageForm.style" :layout="pageForm.layout"
                   @current-change="page" @size-change="changeSize"/>
  </div>
</template>
<script>
import {deleteAll, deleteOne, page} from "@/api/system/log";
import BdpPagination from "@/views/common/bdpPagination";
import {createPageRequestBody} from "@/api/system/pagination";

export default {
  name: "SystemLogIndex",

  components: {BdpPagination},

  data() {
    return {
      tableForm: {},
      pageForm: createPageRequestBody(),
      tableList: [],
    }
  },

  mounted() {
    this.page()
  },

  methods: {
    _delete(id) {
      this.$confirm('确定删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteOne(id).then(() => {
          this.$message({type: 'success', message: '删除成功'})
          this.page()
        })
      }).catch(() => {
        this.$message({type: 'info', message: '删除取消'})
      })
    },

    deleteAll() {
      this.$confirm('确定清空？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteAll().then(() => {
          this.$message({type: 'success', message: '清空成功'})
          this.page()
        })
      }).catch(() => {
        this.$message({type: 'info', message: '清空取消'})
      })
    },

    page(current = 1) {
      this.pageForm.current = current
      page(this.tableForm, this.pageForm.current, this.pageForm.size).then(res => {
        this.tableList = res.data.records
        this.pageForm.total = res.data.total
      })
    },
    changeSize(size) {
      this.pageForm.size = size
      this.page(1)
    },
    resetPageRequestBody() {
      this.tableForm = {}
      this.pageForm = createPageRequestBody()
      this.page()
    },
  },
}
</script>

<style scoped>

</style>
